<template>
  <div class="my-tag" @dblclick="changInput">
    <input
      v-if="isInput"
      v-model="TagText"
      class="input"
      type="text"
      placeholder="输入标签"
      v-getFocuns
      @blur="removeInput"
    />
    <div v-else class="text">{{ value }}</div>
  </div>
</template>

<script>
export default {
  name: "MyTag",
  props: ["value"],
  data() {
    return {
      isInput: false,
      TagText: "",
    };
  },
  methods: {
    // 双击之后使input框显示并把tag值复制给它
    changInput() {
      this.isInput = true;
      this.TagText = this.value;
    },
    // 失去焦点
    removeInput() {
      this.isInput = false;
      // v-model语法糖 值发生改变的时候通知父组件修改对应的值
      this.$emit("input", this.TagText);
    },
  },
  // 封装自定义指令
  directives: {
    getFocuns: {
      inserted(e) {
        // e为绑定改指令的元素
        e.focus();
      },
    },
  },
};
</script>

<style lang="less">
.my-tag {
  cursor: pointer;
  .input {
    appearance: none;
    outline: none;
    border: 1px solid #ccc;
    width: 100px;
    height: 40px;
    box-sizing: border-box;
    padding: 10px;
    color: #666;
    &::placeholder {
      color: #666;
    }
  }
}
</style>